library(tidyverse)
library(sandwich)
library(lmtest)

setwd("~/Dropbox/Collaboration-Grants")

#Load data
newdata111_org <- read.csv("replicationdata.csv", header = TRUE)


#Table 1
model01 <- lm(logobligated ~ evc_std + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(model01)

#Robust standard errors
model01$newse <- vcovHC(model01)
coeftest(model01, model01$newse)


#Table 2 Total Grants
model02 <- lm(index ~ evc_std + deleg_size2 + singlemember +
                  capital + majority + votepctp + seniority + les +
                  totalPopBirthPlace + under25k + 
                  prcntUnemp + medianIncome + prcntHS,
                data = newdata111_org)
summary(model02)

#Robust standard errors
model02$newse <- vcovHC(model02)
coeftest(model02, model02$newse)


#Table 2 Average Grant
model03 <- lm(logavgobligated ~ evc_std + deleg_size2 + singlemember +
                  capital + majority + votepctp + seniority + les +
                  totalPopBirthPlace + under25k + 
                  prcntUnemp + medianIncome + prcntHS,
                data = newdata111_org)
summary(model03)

#Robust standard errors
model03$newse <- vcovHC(model03)
coeftest(model03, model03$newse)


#Table 3 
modelrb01 <- lm(logobligated ~ stdletterevc + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelrb01)


#Table 4
newmembers <- subset(newdata111_org, newdata111_org$seniority == 1)
oldmembers <- subset(newdata111_org, newdata111_org$seniority > 1)

#Table 4 Model 5
modelrb02 <-lm(logobligated ~ evc110,
               data = newmembers)
summary(modelrb02)

#Table 4 Model 6
modelrb03 <- lm(logobligated ~ evc110 + deleg_size2 + singlemember +
                  capital + majority + votepctp + les +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newmembers)
summary(modelrb03)

#Table 4 Model 7
modelrb04 <-lm(logobligated ~ evc110,
               data = oldmembers)
summary(modelrb04)

#Table 4 Model 8
modelrb05 <- lm(logobligated ~ evc110 + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = oldmembers)
summary(modelrb05)


#Supplemental Appendix Models
#SA Table 1 Model 1
modelsa01 <- lm(logobligated ~ cospindeg + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelsa01)

#SA Table 1 Model 2
modelsa02 <- lm(logavgobligated ~ cospindeg + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelsa02)

#SA Table 1 Model 3
modelsa03 <- lm(index ~ cospindeg + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k + 
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelsa03)

#SA Table 2 Model 4
modelsa04 <- lm(logobligated ~ degree + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k +
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelsa04)

#SA Table 2 Model 5
modelsa05 <- lm(logobligated ~ wdegree + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k +
                  prcntUnemp + prcntHS,
                data = newdata111_org)
summary(modelsa05)

#SA Table 2 Model 6
modelsa06 <- lm(logobligated ~ wdegree + deleg_size2 + singlemember +
                  capital + majority + votepctp + les + seniority +
                  totalPopBirthPlace + medianIncome + under25k +
                  prcntUnemp + prcntHS + lettercount,
                data = newdata111_org)
summary(modelsa06)
